{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "respective-generation",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pandas as pd\n",
    "import  matplotlib.pyplot as plt\n",
    "import json\n",
    "import datetime\n",
    "\n",
    "headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}  \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "facial-retail",
   "metadata": {},
   "outputs": [],
   "source": [
    "url=\"http://hqhk.niuguwang.com/hkquote/quotedata/markkline.ashx?code=2000&type=5&count=60\"\n",
    "#req=requests.post(url,headers=headers)\n",
    "html = requests.get(url, headers=headers)\n",
    "df = pd.DataFrame(html.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "exposed-samoa",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"test1.json\",\"w\",encoding=\"utf-8\") as f:\n",
    "    f.write(html.text)\n",
    "\n",
    "with open(\"test1.json\",\"r\",encoding=\"utf-8\") as f:\n",
    "    info=f.read()\n",
    "    data_list = json.loads(info)\n",
    "    brother_info = data_list[\"timedata\"]\n",
    "    df=pd.DataFrame(brother_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "hybrid-stadium",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'df_timedata' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-bae1634bbb78>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf_timedata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'df_timedata' is not defined"
     ]
    }
   ],
   "source": [
    "df_timedata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "round-plaintiff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>times</th>\n",
       "      <th>highp</th>\n",
       "      <th>openp</th>\n",
       "      <th>lowp</th>\n",
       "      <th>nowv</th>\n",
       "      <th>preclose</th>\n",
       "      <th>curvol</th>\n",
       "      <th>curvalue</th>\n",
       "      <th>updownrate</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>2933613</td>\n",
       "      <td>2921946</td>\n",
       "      <td>2903372</td>\n",
       "      <td>2912441</td>\n",
       "      <td>2911320</td>\n",
       "      <td>172742867238</td>\n",
       "      <td>172742867238</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-05-27</td>\n",
       "      <td>2915795</td>\n",
       "      <td>2899839</td>\n",
       "      <td>2895072</td>\n",
       "      <td>2911320</td>\n",
       "      <td>2916601</td>\n",
       "      <td>250501852894</td>\n",
       "      <td>250501852894</td>\n",
       "      <td>-0.18%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-05-26</td>\n",
       "      <td>2926186</td>\n",
       "      <td>2905628</td>\n",
       "      <td>2902795</td>\n",
       "      <td>2916601</td>\n",
       "      <td>2891086</td>\n",
       "      <td>164296338472</td>\n",
       "      <td>164296338472</td>\n",
       "      <td>0.88%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-05-25</td>\n",
       "      <td>2892924</td>\n",
       "      <td>2846150</td>\n",
       "      <td>2846150</td>\n",
       "      <td>2891086</td>\n",
       "      <td>2841226</td>\n",
       "      <td>167870077668</td>\n",
       "      <td>167870077668</td>\n",
       "      <td>1.75%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-05-24</td>\n",
       "      <td>2844436</td>\n",
       "      <td>2841799</td>\n",
       "      <td>2819552</td>\n",
       "      <td>2841226</td>\n",
       "      <td>2845844</td>\n",
       "      <td>120884778639</td>\n",
       "      <td>120884778639</td>\n",
       "      <td>-0.16%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       times    highp    openp     lowp     nowv preclose        curvol  \\\n",
       "0 2021-05-28  2933613  2921946  2903372  2912441  2911320  172742867238   \n",
       "1 2021-05-27  2915795  2899839  2895072  2911320  2916601  250501852894   \n",
       "2 2021-05-26  2926186  2905628  2902795  2916601  2891086  164296338472   \n",
       "3 2021-05-25  2892924  2846150  2846150  2891086  2841226  167870077668   \n",
       "4 2021-05-24  2844436  2841799  2819552  2841226  2845844  120884778639   \n",
       "\n",
       "       curvalue updownrate  \n",
       "0  172742867238        NaN  \n",
       "1  250501852894     -0.18%  \n",
       "2  164296338472      0.88%  \n",
       "3  167870077668      1.75%  \n",
       "4  120884778639     -0.16%  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.times=df.times.apply(lambda x: datetime.datetime.strptime(x[:-6], \"%Y%m%d\"))\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "specified-venice",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_list = json.loads(html.text)\n",
    "timedata = data_list[\"timedata\"]\n",
    "df_timedata=pd.DataFrame(timedata)\n",
    "df_timedata.times=df_timedata.times.apply(lambda x:  datetime.datetime.strptime(x[:-6], \"%Y%m%d\"))\n",
    "df_timedata.index = df_timedata.times\n",
    "df_timedata.set_index(\"times\", drop=True,inplace=True)\n",
    "df_timedata.head()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
